﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MKAdminCore.DTO.Web.Common;
using MKAdminCore.DTO.Web.HomePage.Pandect;
using MKAdminCore.DTO.Web.Login;
using MKAdminCore.IService.Web.HomePage;
using MKAdminCore.ServiceImpl.Web.Common;
using MKAdminCore.ToolKit;
using Dapper;
using MKAdminCore.ToolKit.FileHelper;

namespace MKAdminCore.ServiceImpl.Web.HomePage
{
    public class PandectService : IPandectService
    {
        /// <summary>
        /// 获取tab内容
        /// </summary>
        /// <returns></returns>
        public async Task<HttpResponseResult<List<GetTabContentModel>>> GetTabContent(GetTabContentParameter parameter)
        {
            var list = new HttpResponseResult<List<GetTabContentModel>>()
            {
                Data = new List<GetTabContentModel>()
            };
            using (var conn = new SqlConnection(AppSettingsJsonInfo.ConnectionStrings.ReadConnStr))
            {
                string sql = $@"
                                Select RightId TabId,SoundCodeFileName TabContentTitle
                                From dbo.PageSoundCodeRightInfo pscri With(NoLock)
                                Where RightId = {parameter.TabId} And StatusCode = 0
                                Order By OrderNo Desc";

                var result = await conn.QueryAsync<GetTabContentModel>(sql);

                list.Data = result.ToList();

                if (list.Data != null && list.Data.Count > 0)
                {
                    list.Data.ForEach((q) =>
                    {
                        var fileName = q.TabContentTitle;
                        var extensionName = System.IO.Path.GetExtension(fileName);
                        switch (extensionName)
                        {
                            case ".cshtml":
                                q.CodeType = "html";
                                q.TabIcon = "/images/code_html.png";
                                break;
                            case ".js":
                                q.CodeType = "js";
                                q.TabIcon = "/images/code_js.png";
                                break;
                            case ".cs":
                                q.CodeType = "c#";
                                q.TabIcon = "/images/code_cs.png";
                                break;
                            default:
                                break;
                        }

                        q.TabContentTitle = System.IO.Path.GetFileName(fileName);
                        q.TabContentStr = TxtHelper.GetFileContent(fileName).data;
                    });
                }
            }

            return list;
        }

        /// <summary>
        /// 下载源码日志
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public Result DownLoadCodeLog(UserInfo userInfo)
        {
            var result = new Result();

            return result;
        }


    }
}
